/*
* @Author: dengjiayao
* @Date:   2017-04-10 14:57:41
 * @Last Modified by: songwh
 * @Last Modified time: 2018-02-06 16:35:20
*/

import { modal } from 'vue-strap'
import { showMsg, saveItem , toogleSectionListFresh} from '../../../vuex/action'

export default {
  name: 'item-new',
  components: {
    'vue-modal': modal
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xToogleFresh: toogleSectionListFresh,
      xSaveItem: saveItem,
    }
  },
  props: ['showItemNew', 'it', 'title'],
  data() {
    return {
    }
  },
  computed: {
  },
  ready() {
  },
  methods: {
    submit() {
      let files = this.$els.file.files
      let formData = new FormData()
      let fields = ['data1', 'data2', 'data3','productId', 'itemId']
      for (let k of fields) {
        if (this.it[k]) {
          formData.append(k, this.it[k])
        }
      }
      if(files[0]){
        formData.append('picFile', files[0])
      }
      formData.append('classifyId', this.$route.params.id)
      formData.append('type',this.it.type)
      this.xSaveItem(formData)
        .then((data) => {
          this.showItemNew= false
          this.xShowMsg({
            type: 'danger',
            content: '操作成功！'
          })
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    showItemNew(newVal) {
      if (!newVal) {
        this.xToogleFresh()
      }
    }
  },
}
